<template>
  <div>
    <VRow class="lf-config-row">
      <VCol cols="12">
        <VTextField
          v-model="executorId"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="执行器ID"
          label="执行器ID"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="executorName"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="执行器名称"
          label="执行器名称"
        />
      </VCol>
      <VCol cols="12">
        <VAutocomplete
          v-model="ivyConfigId"
          class="flow-mt-12"
          variant="outlined"
          label="选择配置项"
          :items="configOptions"
          item-title="configName"
          item-value="id"
          clearable
        />
      </VCol>
    </VRow>
  </div>
</template>

<script setup>
import axios from '@axios'
import { ref } from 'vue'

const props = defineProps({		
  currRow: Object,
  addUrl: String,
  updateUrl: String,
})

const emits = defineEmits(['refreshPage'])

const addUrl = ref(props.addUrl)
const updateUrl = ref(props.updateUrl)

const currRow = ref(props.currRow)
const row = currRow.value

const isEdit = ref(!!row.id)

const id = ref(row.id || '')
const executorId = ref(row.executorId || '')
const executorName = ref(row.executorName || '')
const ivyConfigId = ref(row.ivyConfigId || '')

const getUrl = () => {
  if(isEdit.value){
    return updateUrl.value
  }
  
  return addUrl.value
}

const saveItem = () => {
  axios.post(getUrl(), { 
    id: id.value,
    executorId: executorId.value,
    executorName: executorName.value,
    ivyConfigId: ivyConfigId.value,
  }).then(r => {
    emits('refreshPage', r.data)
  }).catch(e => {
    console.error(e)
  })
}



defineExpose({
  saveItem,
})

const configOptions = reactive([])

onMounted(() => {
  axios.post('/liteflow/config/options', {}).then(r => {
    configOptions.push(...r.data.data)
  }).catch(e => {
    console.error(e)
  })
})
</script>

<style scoped>
.lf-config-row .v-col {
  padding-block-end: 3px;
}

.lf-config-item {
  padding-block-start: 8px;
}
</style>
